package action.info;

import javax.servlet.http.HttpServletRequest;

import net.fckeditor.FCKeditor;

import org.apache.struts2.interceptor.ServletRequestAware;

import persistence.model.TdfkfaDoc;
import service.DocService;

import com.opensymphony.xwork2.ActionSupport;

public class DocManageAction extends ActionSupport implements ServletRequestAware {

    private static final long serialVersionUID = -158349609241512691L;

    private HttpServletRequest request;

    private DocService docService;

    private String[] docId;

    private String editorHtml;

    private String editorInstanceName;

    private String docTitle;

    private String did;

    private String publishState;

    public String deleteDoc() throws Exception {

        if (docId == null || docId.length == 0) {
            return INPUT;
        }

        Integer[] ids = new Integer[docId.length];
        for (int i = 0; i < docId.length; i++) {
            ids[i] = new Integer(docId[i]);
        }

        docService.deleteDoc(ids);

        return SUCCESS;
    }

    public String editDoc() throws Exception {
        if (docId == null || docId.length != 1) {
            return INPUT;
        } else {
            did = docId[0];
            request.getSession().setAttribute("did", did);
            return SUCCESS;
        }
    }

    public String prepareEdit() throws Exception {

        editorInstanceName = "docEditor";

        FCKeditor editor = new FCKeditor(request, "docEditor");
        editor.setToolbarSet("Doc");
        editor.setHeight("400px");

        did = (String) request.getSession().getAttribute("did");
        TdfkfaDoc doc = docService.getDoc(new Integer(did));

        if (doc == null) {
            return INPUT;
        }

        editor.setValue(doc.getContent());

        editorHtml = editor.createHtml();

        docTitle = doc.getTitle();
        
        return SUCCESS;
    }

    public void setDocId(String[] docId) {
        this.docId = docId;
    }

    public String getEditorHtml() {
        return editorHtml;
    }

    public String getEditorInstanceName() {
        return editorInstanceName;
    }

    public String getDocTitle() {
        return docTitle;
    }

    public String getDid() {
        return did;
    }

    public String getPublishState() {
        return publishState;
    }

    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    public void setDocService(DocService docService) {
        this.docService = docService;
    }

}
